package band.wukong.practice.netty.userguide.heartbeat;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * Desc:
 * author: wukonggg@139.com
 */
public class HeartbeatHandlerInitializer extends ChannelInitializer<Channel> {

    private static final int READ_IDEL_TIME_OUT = 4;    // 读超时的时间 4 秒
    private static final int WRITE_IDEL_TIME_OUT = 5;   // 写超时的时间 5 秒
    private static final int ALL_IDEL_TIME_OUT = 7;     // 其他所有的超时时间 7 秒


    @Override
    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        // 1、使用了 IdleStateHandler ，分别设置了读、写超时的时间
        pipeline.addLast(new IdleStateHandler(READ_IDEL_TIME_OUT, WRITE_IDEL_TIME_OUT, ALL_IDEL_TIME_OUT, TimeUnit.SECONDS));

        // 2、定义了一个 HeartbeatServerHandler 处理器，用来处理超时时，发送心跳
        pipeline.addLast(new HeartbeatServerHandler());
    }
}